package com.hao.educenter.controller;


import com.hao.commonutils.JwtUtils;
import com.hao.commonutils.R;
import com.hao.commonutils.ordervo.UcenterMemberOrder;
import com.hao.educenter.entity.UcenterMember;
import com.hao.educenter.entity.vo.UcenterMemberVo;
import com.hao.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author hao
 * @since 2020-12-20
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        String token = memberService.loginUser(member);
        return R.ok().data("token", token);
    }


    @PostMapping("register")
    public R registerUser(@RequestBody UcenterMemberVo vo) {
        memberService.registerUser(vo);
        return R.ok();
    }

    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //使用jwt工具类，获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo", member);
    }


    /**
     * 得到用户信息订单,用于订单生成支付模块
     *
     * @param id id
     * @return {@link UcenterMemberOrder}
     */
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        UcenterMemberOrder memberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, memberOrder);
        return memberOrder;
    }

    /**
     * 计算某一天的注册人数
     *
     * @param day 一天
     * @return {@link Integer}
     */
    @GetMapping("countRegister/{day}")
    public Integer countRegister(@PathVariable String day) {
        Integer days = memberService.countRegister(day);
        return days;
    }

}

